﻿
@{
    Layout = "~/Views/Shared/_LayuiLayout.cshtml";
}

@model MenuOutput
@{ var parentList = ViewBag.ParentList as IEnumerable<MenuOutput>; }

<form class="layui-form layui-form-pane">
    @Html.AntiForgeryToken()
    <input type="hidden" name="Id" value="@Model.Id" />
    <!-- 菜单深度（按上级菜单选项赋值） -->
    <input type="hidden" name="MenuDeep" value="@Model.MenuDeep" />

    <div class="layui-form-item">
        <label class="layui-form-label">上级菜单</label>
        <div class="layui-input-block">
            <select name="ParentId" lay-search="" lay-filter="parent">
                <option data-deep="-1" value="0">无</option>
                @if (parentList != null && parentList.Count() > 0)
                {
                    foreach (var item in parentList)
                    {
                <option value="@item.Id" data-deep="@item.MenuDeep" @(Model.ParentId == item.Id ? "selected=\"selected\"" : "")>@item.MenuName</option>
                    }
                }
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">菜单名称</label>
        <div class="layui-input-block">
            <input type="text" class="layui-input" name="MenuName" value="@Model.MenuName" lay-verify="required" autocomplete="off" />
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">菜单片区</label>
        <div class="layui-input-block">
            <input type="text" class="layui-input" name="MenuArea" value="@Model.MenuArea" />
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">菜单控制</label>
        <div class="layui-input-block">
            <input type="text" class="layui-input" name="MenuControl" value="@Model.MenuControl" />
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">菜单方法</label>
        <div class="layui-input-block">
            <input type="text" class="layui-input" name="MenuAction" value="@Model.MenuAction" />
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">菜单参数</label>
        <div class="layui-input-block">
            <input type="text" class="layui-input" name="MenuParam" value="@Model.MenuParam" />
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">菜单图标</label>
        <div class="layui-input-block">
            <input type="text" class="layui-input" name="MenuIcon" value="@Model.MenuIcon" autocomplete="off" />
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">菜单排序</label>
        <div class="layui-input-block">
            <input type="text" class="layui-input" name="SortIndex" value="@Model.SortIndex" />
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">是否启用</label>
        <div class="layui-input-block">
            <input type="checkbox" class="layui-input" title="启用" name="IsEnable" value="True" @(Model.IsEnable ? "checked=\"checked\"" : "") />
        </div>
    </div>

    <div class="layui-form-item" align="center">
        <button type="button" class="layui-btn" lay-submit="" lay-filter="Save">保存</button>
        <button type="button" class="layui-btn layui-btn-primary" id="btnCancel">取消</button>
    </div>
</form>

@section scripts{
<script type="text/javascript">
    var $, layer;
    layui.use(['form'], function () {
        var form = layui.form;
        $ = layui.jquery;
        layer = parent.layer === undefined ? layui.layer : parent.layer;
        
        // 菜单深度
        var deep = 0;
        deep = parseInt($('select[name=ParentId] option:selected').data('deep')) + 1;
        $('input[name=MenuDeep]').val(deep);
        form.on('select(parent)', function (data) {
            var elem = $(data.elem);
            deep = parseInt(elem.find('option:selected').data('deep')) + 1;
            $('input[name=MenuDeep]').val(deep);
        });

        // 监听关闭
        $('#btnCancel').on('click', function () {
            layer_close();
        });

        // 监听提交
        form.on('submit(Save)', function (data) {
            var loading = layer.load();
            $.post('@Url.Action("MenuAddOrEdit", "Menu")', data.field,
                function (result) {
                    layer.close(loading);
                    if (result.Success) {
                        layer.msg(result.Message, { icon: 1, time: 2000 });
                        parent.reload();
                        layer_close();
                    } else {
                        layer.alert(result.Message, { icon: 2 });
                    }
                });
            return false;
        })


    });
</script>
    }


